#/usr/bin/env python3

import paramiko
import sys
import argparse
import csv
import logging

def arg_parse():
    parser = argparse.ArgumentParser()
    parser.add_argument('-f','--filename',help='ssh connnection info file',required=True)
    args = parser.parse_args()
    return args

def ssh_dev(l_conn, cmd):
    try:
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname=l_conn[0],port=l_conn[1],username=l_conn[2],password=l_conn[3],timeout=10)
        stdin, stdout, stderr = client.exec_command(cmd)
        ssh_out = stdout.read()
        ssh_err = stderr.read()
        client.close()
    except Exception as e:
        client.close()
        mesg = l_conn[0] + " [-] " + str(e)
        logging.error(mesg)
        return 1
    if ssh_err:
        mesg = l_conn[0] + " [-] " + ssh_err
        logging.error(mesg)
        return 2

if __name__ == '__main__':
    args = arg_parse()
    filename = args.filename
    logging.basicConfig(level=logging.INFO, format='%(asctime)s :: %(levelname)s :: %(message)s')
    with open(filename,'r') as f:
        reader = csv.reader(f)
        for row in reader:
            print ("%s" %(row[0]))
            cmd = 'rm -rf /etc/localtime; ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime;ntpdate 11.3.11.1;hwclock -w'
            ssh_dev(row, cmd)
